package com.power.using.mydaggerp.presenter.home.tabs;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.orhanobut.logger.Logger;
import com.power.using.mydaggerp.constant.BundleKeyConstant;
import com.power.using.mydaggerp.contract.home.tabs.WangyiContract;
import com.power.using.mydaggerp.model.bean.wangyi.WangyiNewsItemBean;
import com.power.using.mydaggerp.model.bean.wangyi.WangyiNewsListBean;
import com.power.using.mydaggerp.model.home.tabs.WangyiModel;
import com.power.using.mydaggerp.ui.activity.detial.WangyiDailyDetailActivity;
import com.zyw.horrarndoo.sdk.utils.StringUtils;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * Created by 聪明一只哈 on 2019/10/11.
 * Time: 2019/10/11  14:17
 */
public class WangyiPresenter extends WangyiContract.WangyiPresenter{

    private int mCurrentIndex;
    private boolean isLoading;

    @NonNull
    public static WangyiPresenter newInstance() {
        return new WangyiPresenter();
    }

    @Override
    protected WangyiContract.IWangyiModel getModel() {
        return WangyiModel.newInstance();
    }

    @Override
    public void loadLatestList() {

        mCurrentIndex=0;
        mRxManage.register(mIModel.getNewsList(mCurrentIndex).subscribe(new Consumer<WangyiNewsListBean>() {
            @Override
            public void accept(WangyiNewsListBean wangyiNewsListBean) throws Exception {
                if (mIView != null) {

                    List<WangyiNewsItemBean> list = wangyiNewsListBean.getNewsList();
                    for (int i = 0; i < list.size(); i++) {
                        if (StringUtils.isEmpty(list.get(i).getUrl())) {
                            list.remove(i);
                        }
                    }
                    mCurrentIndex += 20;
                    mIView.updateContentList(list);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(mIView!=null){
                    if(mIView.isVisiable()){
                        mIView.showToast("NetWork error.");
                    }
                    mIView.showNetworkError();
                }
            }
        }));

    }

    @Override
    public void loadMoreList() {

        if(!isLoading){
            isLoading=true;
            mRxManage.register(mIModel.getNewsList(mCurrentIndex).subscribe(new Consumer<WangyiNewsListBean>() {
                @Override
                public void accept(WangyiNewsListBean wangyiNewsListBean) throws Exception {
                    isLoading = false;
                    if (mIView == null)
                        return;
                    if (wangyiNewsListBean.getNewsList().size() > 0) {
                        mCurrentIndex += 20;
                        mIView.updateContentList(wangyiNewsListBean.getNewsList());
                    } else {
                        mIView.showNoMoreData();
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    isLoading=false;
                    if(mIView==null){
                        return;
                    }
                    mIView.showLoadMoreError();
                }
            }));
        }

    }

    @Override
    public void onItemClick(final int position, WangyiNewsItemBean item) {

        mRxManage.register(mIModel.recordItemIsRead(item.getDocid()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {

                if(mIView==null){
                    return;
                }
                if(aBoolean){
                    mIView.itemNotifyChanged(position);
                }else{
                    Logger.e("写入点击状态值失败");
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
            }
        }));

        if(mIView==null){
            return;
        }


        Bundle bundle = new Bundle();
        bundle.putString(BundleKeyConstant.ARG_KEY_WANGYI_DETAIL_ID, item.getDocid());
        bundle.putString(BundleKeyConstant.ARG_KEY_WANGYI_DETAIL_URL, item.getUrl());
        bundle.putString(BundleKeyConstant.ARG_KEY_WANGYI_DETAIL_TITLE, item.getTitle());
        bundle.putString(BundleKeyConstant.ARG_KEY_WANGYI_DETAIL_IMAGE_URL, item.getImgsrc());
        bundle.putString(BundleKeyConstant.ARG_KEY_WANGYI_DETAIL_COPYRIGHT, item.getSource());
        mIView.startNewActivity(WangyiDailyDetailActivity.class, bundle);

    }



    @Override
    public void onStart() {

    }
}
